<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/4/1
 * Time: 15:41
 */

namespace SDXApi\Service;


use SDXApi\Adapters\SeminarAdapter;
use SDXApi\Model\SeminarModel;
use SDXApi\Model\SeminarPageModel;

/**
 * Class SeminarsService
 * @package SDXApi\Service
 */
class SeminarsService
{

	/**
	 * @var SeminarModel
	 */
	protected $seminarModel;

	/**
	 * @var SeminarPageModel
	 */
	protected $seminarPageModel;

	/**
	 * @var int
	 */
	public $is_show = 0;

	/**
	 * @var int
	 */
	public $userId = 0;

	/**
	 * @var int
	 */
	public $wid = 8;

	/**
	 * SeminarsService constructor.
	 */
	public function __construct()
	{
		$this->seminarModel = new SeminarModel();
		$this->seminarPageModel = new SeminarPageModel();
	}

    /**
     * @author Min
     * @param $id
     * @param int $pageNumber
     * @param int $pageSize
     * @return array
     */
	public function findSeminarTemplatePageById($id, $pageNumber = 0, $pageSize = 0)
	{

		$template = $this->findSeminarTemplateById($id);
		$page = $this->findSeminarPagesByTemplateId($template['id'], $pageNumber, $pageSize);

//		$phone = $this->getUserPhoneBind($this->userId);
//
//		if (empty($phone)) {
//		    $page['bind_phone'] = 0;
//        }
//        else{
//		    $page['bind_phone'] = 1;
//        }

		if (empty($page)) {
			return array();
		}

		$data = $this->checkSeminarTemplatePages($page);

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param $id
	 * @return mixed
	 */
	protected function findSeminarTemplateById($id)
	{
		return $this->seminarModel->find_seminar_template_by_id($id, $this->is_show);
	}

    /**
     * @author Min
     * @param $id
     * @param int $pageNumber
     * @param int $pageSize
     * @return mixed
     */
	protected function findSeminarPagesByTemplateId($id, $pageNumber = 0, $pageSize = 0)
	{
		return $this->seminarPageModel->find_seminar_template_pages_by_template_id($id, $pageNumber, $pageSize);
	}

	/**
	 * Created by Jerry
	 * @param $pages
	 * @return array
	 */
	protected function checkSeminarTemplatePages($pages)
	{
		$data = array();
		foreach ($pages as $key => $value) {
			$seminar = $this->seminar($value['type'])->handle($value, $this->userId, $this->wid);

			if (empty($seminar)) {
				continue;
			}

			$seminar['border_size'] = 20;

			$data[] = $seminar;
		}

		if (empty($data)) {
			$data = array();
		}

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param int $type
	 * @return SeminarAdapter
	 */
	protected function seminar($type = 0)
	{
		return new SeminarAdapter($type);
	}

	/**
	 * Created by Jerry
	 * @param int $id
	 * @return mixed
	 */
	public function getTemplateById($id = 0)
	{
		if (empty($id)) {
			return false;
		}
		return $this->seminarModel->find_seminar_template_by_id($id, $this->is_show);
	}

    /**
     * Created by Jerry
     * @param int $userId
     * @return bool
     */
	protected function getUserPhoneBind($userId = 0)
    {
        $phone = D('Users')->findUserPhone($userId);

        if (empty($phone)) {
            return false;
        }

        return true;
    }

}